package newx.com.control.text
{
	import flash.text.TextField;
	import flash.text.TextFormat;

	public class NxTextFormatManager
	{
		public function NxTextFormatManager()
		{
		}
		
		/**
		 * 将 newVal 合并到老值值oldVal中。
		 * @param newVal
		 * @param oldVal
		 * 
		 */		
		public static function setFormatToOld(newVal:TextFormat,oldVal:TextFormat):void
		{
			if(newVal	==	null)
			{
				oldVal	=	new TextFormat();
			}
			else
			{
				setFormatValToOld(newVal,oldVal,"align");
				setFormatValToOld(newVal,oldVal,"blockIndent");
				setFormatValToOld(newVal,oldVal,"bold");
				setFormatValToOld(newVal,oldVal,"bullet");
				setFormatValToOld(newVal,oldVal,"color");
				setFormatValToOld(newVal,oldVal,"constructor");
				setFormatValToOld(newVal,oldVal,"font");
				setFormatValToOld(newVal,oldVal,"indent");
				setFormatValToOld(newVal,oldVal,"italic");
				setFormatValToOld(newVal,oldVal,"kerning");
				setFormatValToOld(newVal,oldVal,"leading");
				setFormatValToOld(newVal,oldVal,"leftMargin");
				setFormatValToOld(newVal,oldVal,"letterSpacing");
				setFormatValToOld(newVal,oldVal,"rightMargin");
				setFormatValToOld(newVal,oldVal,"size");
				setFormatValToOld(newVal,oldVal,"underline");
				setFormatValToOld(newVal,oldVal,"url");
			}
		}
		
		public static function setFormatVal(val:TextFormat,valName:String,valData:Object):void
		{
			if(val == null)
			{
				val	=	new TextFormat();
				val[valName]	=	valData;
			}
		}
		
		public static function setFormatValToOld(newVal:TextFormat,oldVal:TextFormat,valName:String):void
		{
			if(newVal == null || oldVal == null || String(valName) == "")
			{
				return;
			}
			else
			{
				if(newVal[valName] != oldVal[valName])
				{
					oldVal[valName]	=	newVal[valName];
				}
			}
		}
		
		/**
		 * 更新文本控件格式 
		 * @param txt
		 * @param format
		 * @param beginIndex
		 * @param endIndex
		 * 
		 */		
		public static function updataTextFormat(txt:TextField,
												format:TextFormat,
												beginIndex:int=-1,
												endIndex:int=-1):void
		{
			txt.defaultTextFormat	=	format;
			txt.setTextFormat(format, beginIndex, endIndex);
		}
	}
}